import { createRouter, createWebHistory } from 'vue-router';
import routes from './routes'
import { Session } from '@/util/sessionStorageUtil';

const routerHistory = createWebHistory();

const router = createRouter({
    history: routerHistory,
    routes: routes
})

router.beforeEach((to, from, next) => {

    if (to.meta.title) {
        document.title = to.meta.title as string
    }
    const token: string = Session.get("token");

    if (to.path === '/login' && !token) {
        next();
    } else {
        if (!token) {
            Session.clear();
            next('/login');
        } else if (to.path === '/login' && token) {
            next('/');
        } else {
            next();
        }
    }

})

export default router